package com.frozenlab.android.weather.helper;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.frozenlab.android.weather.activity.R;
import com.frozenlab.android.weather.vo.ForecastVO;
import com.frozenlab.android.weather.vo.WeatherVO;

public class WundergroundWeatherHelpher {
	private static final String TAG = "WundergroundWeatherHelpher";
	
	public static final Map<String, Integer> weatherImageMap = new HashMap<String, Integer>(){{
		put("chanceflurries", R.drawable.chanceflurries);
		put("chancerain", R.drawable.chancerain);
		put("chancesleet", R.drawable.chancesleet);
		put("chancesnow", R.drawable.chancesnow);
		put("chancetstorms", R.drawable.chancetstorms);
		put("clear", R.drawable.clear);
		put("cloudy", R.drawable.cloudy);
		put("flurries", R.drawable.flurries);
		put("fog", R.drawable.fog);
		put("hazy", R.drawable.hazy);
		put("mostlycloudy", R.drawable.mostlycloudy);
		put("mostlysunny", R.drawable.mostlysunny);
		put("partlycloudy", R.drawable.partlycloudy);
		put("partlysunny", R.drawable.partlysunny);
		put("sleet", R.drawable.sleet);
		put("rain", R.drawable.rain);
		put("snow", R.drawable.snow);
		put("sunny", R.drawable.sunny);
		put("tstorms", R.drawable.tstorms);
		put("unknown", R.drawable.unknown);
	}};
	
	public static final Map<String, Integer> weatherBackgroundMap = new HashMap<String, Integer>(){{
		put("chanceflurries", R.drawable.background_flurries);
		put("chancerain", R.drawable.background_rain);
		put("chancesleet", R.drawable.background_sleet);
		put("chancesnow", R.drawable.background_snow);
		put("chancetstorms", R.drawable.background_tstorms);
		put("clear", R.drawable.background_clear);
		put("cloudy", R.drawable.background_cloudy);
		put("flurries", R.drawable.background_flurries);
		put("fog", R.drawable.background_fog);
		put("hazy", R.drawable.background_hazy);
		put("mostlycloudy", R.drawable.background_cloudy);
		put("mostlysunny", R.drawable.background_sunny);
		put("partlycloudy", R.drawable.background_cloudy);
		put("partlysunny", R.drawable.background_sunny);
		put("sleet", R.drawable.background_sleet);
		put("rain", R.drawable.background_rain);
		put("snow", R.drawable.background_snow);
		put("sunny", R.drawable.background_sunny);
		put("tstorms", R.drawable.background_tstorms);
		put("unknown", R.drawable.background_unknown);
	}};
	
	
	public static String createUrl(String latitude, String longitude) {
		/* contruct url to use wunderground weather (hardcoded temporary for test)
		 * for details see http://www.wunderground.com/weather/api/d/documentation.html
		 * sample json format http://api.wunderground.com/api/f613ca4434f62727/geolookup/conditions/forecast/q/3.1358104166666667,101.615525316666664.json
		*/
		String strUrl = "http://api.wunderground.com/api/f613ca4434f62727/geolookup/conditions/forecast/q/"
				+ latitude + "," + longitude + ".json";
			
		return strUrl;
	}

	public static WeatherVO parseWundergroundWeatherJSON(String strData) {
		//verify parameter
		if (strData == null){
			Log.e(TAG,"PARAM is NULL");
			return null;
		}
		
		WeatherVO weatherVO = new WeatherVO();
		String conditionTemperature = null;
		String city = null;
		String conditionText = null;
		String conditionCode = null;
		String dateLastUpdate = null;
		
		try {
			JSONObject root = new JSONObject(strData);

			conditionTemperature = root.getJSONObject("current_observation").getString("temp_f");
			city = root.getJSONObject("current_observation").getJSONObject("display_location").getString("city");
			conditionText = root.getJSONObject("current_observation").getString("weather");
			conditionCode = root.getJSONObject("current_observation").getString("icon");
			dateLastUpdate  = root.getJSONObject("current_observation").getString("local_time_rfc822");
			
			weatherVO.setCity(city);
			weatherVO.setConditionTemperature(conditionTemperature);
			weatherVO.setConditionText(conditionText);
			weatherVO.setConditionCode(conditionCode);
			weatherVO.setConditionDate(dateLastUpdate);
			
			JSONObject simpleforecast = root.getJSONObject("forecast").getJSONObject("simpleforecast");
			JSONArray forecastdayArray = new JSONArray();
			forecastdayArray = simpleforecast.getJSONArray("forecastday");

			Log.e(TAG, "Number of entries " + forecastdayArray.length());
			
			//Create Forecast Array and point to weatherVO.ForecastVOArray
			ForecastVO[] forecastVOArray = weatherVO.getForecastVOArray();
			
			for (int i = 0; i < forecastdayArray.length(); i++) {
				JSONObject temp = (JSONObject) forecastdayArray.get(i);
				forecastVOArray[i].setForecastDay(temp.getJSONObject("date").getString("weekday_short"));
				forecastVOArray[i].setForecastHigh(temp.getJSONObject("high").getString("fahrenheit"));
				forecastVOArray[i].setForecastLow(temp.getJSONObject("low").getString("fahrenheit"));
				forecastVOArray[i].setForecastText(temp.getString("conditions"));
				forecastVOArray[i].setForecastCode(temp.getString("icon"));
			}
		} catch (Exception e) {
			Log.e(TAG, e.toString());
		}

		return weatherVO;
	}
	
	public static String convertFahrenheitToCelcius(String temp){
		double F = Double.parseDouble(temp);
		double C = (F-32)*5/9;
		C = (double)Math.round(C *10) /10;
		
		return Double.toString(C);
	}

}
